package com.paas.form.service.impl;

import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.common.web.service.BaseServiceImpl;
import com.paas.form.dao.FormGroupDao;
import com.paas.form.entity.FormGroupEntity;
import com.paas.form.entity.FormViewEntity;
import com.paas.form.enums.GroupTypeEnums;
import com.paas.form.service.IFormGroupService;
import com.paas.form.service.IFormViewService;
import com.paas.form.vo.FormGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("formGroupService")
public class FormGroupServiceImpl extends BaseServiceImpl<FormGroupDao, FormGroupEntity> implements IFormGroupService {

    @Autowired
    private IFormViewService formViewService;

    /**
     * 查询分组-视图树
     * @return {@link List}<{@link FormGroupVo}>
     */
    @Override
    public List<FormGroupVo> queryGroupAndViewTree() {
        List<FormGroupVo> groupVos = new ArrayList<>();
        List<FormGroupEntity> groupEntities = this.list();
        if (BeanUtil.isEmpty(groupEntities)) {
            return groupVos;
        }
        //groupEntities转map
        Map<String, FormGroupEntity> groupEntityMap = groupEntities.stream().collect(Collectors.toMap(FormGroupEntity::getId, groupEntity -> groupEntity));
        List<FormViewEntity> viewEntities = formViewService.list();
        //groupEntities 过滤出根节点 parentId = null
        List<FormGroupEntity> rootGroups = groupEntities.stream().filter(groupEntity -> StringUtil.isEmpty(groupEntity.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(rootGroups)) {
            //递归查询子节点
            for (FormGroupEntity rootGroup : rootGroups) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(rootGroup.getId());
                groupVo.setCode(rootGroup.getCode());
                groupVo.setName(rootGroup.getName());
                groupVo.setPath(rootGroup.getPath());
                groupVo.setRemark(rootGroup.getRemark());
                groupVo.setParentId(rootGroup.getParentId());
                String parentName = StringUtil.isNullOrEmpty(rootGroup.getParentId()) ? "" : groupEntityMap.get(rootGroup.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                groupVo.setChildren(getChildrenGroupAndView(rootGroup, groupEntities, viewEntities, groupEntityMap));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    @Override
    public List<FormGroupVo> queryGroupTree() {
        List<FormGroupVo> groupVos = new ArrayList<>();
        List<FormGroupEntity> groupEntities = this.list();
        if (BeanUtil.isEmpty(groupEntities)) {
            return groupVos;
        }
        //groupEntities转map
        Map<String, FormGroupEntity> groupEntityMap = groupEntities.stream().collect(Collectors.toMap(FormGroupEntity::getId, groupEntity -> groupEntity));

        List<FormGroupEntity> rootGroups = groupEntities.stream().filter(groupEntity -> StringUtil.isEmpty(groupEntity.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(rootGroups)) {
            for (FormGroupEntity rootGroup : rootGroups) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(rootGroup.getId());
                groupVo.setCode(rootGroup.getCode());
                groupVo.setName(rootGroup.getName());
                groupVo.setPath(rootGroup.getPath());
                groupVo.setRemark(rootGroup.getRemark());
                groupVo.setParentId(rootGroup.getParentId());
                String parentName = StringUtil.isNullOrEmpty(rootGroup.getParentId()) ? "" : groupEntityMap.get(rootGroup.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                groupVo.setChildren(getChildrenGroup(rootGroup, groupEntities, groupEntityMap));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    /**
     * 递归查询子节点-分组
     * @param groupEntity   组实体
     * @param groupEntities 组实体
     * @return {@link List}<{@link FormGroupVo}>
     */
    private List<FormGroupVo> getChildrenGroup(FormGroupEntity groupEntity, List<FormGroupEntity> groupEntities, Map<String, FormGroupEntity> groupEntityMap) {
        List<FormGroupVo> groupVos = new ArrayList<>();
        //过滤出子分组
        List<FormGroupEntity> children =
                groupEntities.stream().filter(group -> groupEntity.getId().equals(group.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(children)) {
            for (FormGroupEntity child : children) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(child.getId());
                groupVo.setCode(child.getCode());
                groupVo.setName(child.getName());
                groupVo.setPath(child.getPath());
                groupVo.setRemark(child.getRemark());
                groupVo.setParentId(groupEntity.getId());
                String parentName = StringUtil.isNullOrEmpty(child.getParentId()) ? "" : groupEntityMap.get(child.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                groupVo.setChildren(getChildrenGroup(child, groupEntities, groupEntityMap));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    /**
     * 递归查询子节点-分组-视图
     * @param groupEntity   组实体
     * @param groupEntities 组实体
     * @param viewEntities  视图实体
     * @return {@link List}<{@link FormGroupVo}>
     */
    private List<FormGroupVo> getChildrenGroupAndView(FormGroupEntity groupEntity, List<FormGroupEntity> groupEntities,
                                                      List<FormViewEntity> viewEntities, Map<String, FormGroupEntity> groupEntityMap) {
        List<FormGroupVo> groupVos = new ArrayList<>();
        //过滤出子分组
        List<FormGroupEntity> children =
                groupEntities.stream().filter(group -> groupEntity.getId().equals(group.getParentId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(children)) {
            for (FormGroupEntity child : children) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(child.getId());
                groupVo.setCode(child.getCode());
                groupVo.setName(child.getName());
                groupVo.setPath(child.getPath());
                groupVo.setRemark(child.getRemark());
                groupVo.setParentId(groupEntity.getId());
                String parentName = StringUtil.isNullOrEmpty(child.getParentId()) ? "" : groupEntityMap.get(child.getParentId()).getName();
                groupVo.setParentName(parentName);
                groupVo.setType(GroupTypeEnums.GROUP.getCode());
                groupVo.setTypeName(GroupTypeEnums.GROUP.getValue());
                groupVo.setChildren(getChildrenGroupAndView(child, groupEntities, viewEntities, groupEntityMap));
                groupVos.add(groupVo);
            }
        }
        //过滤出主数据
        List<FormViewEntity> views = viewEntities.stream().filter(view -> groupEntity.getId().equals(view.getGroupId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(views)) {
            for (FormViewEntity view : views) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(view.getId());
                groupVo.setCode(view.getCode());
                groupVo.setName(view.getName());
                groupVo.setType(GroupTypeEnums.VIEW.getCode());
                groupVo.setTypeName(GroupTypeEnums.VIEW.getValue());
                groupVo.setParentId(groupEntity.getId());
                String parentName = StringUtil.isNullOrEmpty(view.getGroupId()) ? "" : groupEntityMap.get(view.getGroupId()).getName();
                groupVo.setParentName(parentName);
                view.setGroupName(parentName);
                groupVo.setView(view);
                //获取主数据的子集
                groupVo.setChildren(getChildrenView(view, viewEntities));
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }

    /**
     * 递归查询子节点-视图
     * @param view         视图
     * @param viewEntities 视图实体
     * @return {@link List}<{@link FormGroupVo}>
     */
    private List<FormGroupVo> getChildrenView(FormViewEntity view, List<FormViewEntity> viewEntities) {
        List<FormGroupVo> groupVos = new ArrayList<>();
        List<FormViewEntity> children = viewEntities.stream().filter(viewEntity -> view.getId().equals(viewEntity.getViewId())).collect(Collectors.toList());
        if (BeanUtil.isNotEmpty(children)) {
            for (FormViewEntity child : children) {
                FormGroupVo groupVo = new FormGroupVo();
                groupVo.setId(child.getId());
                groupVo.setCode(child.getCode());
                groupVo.setName(child.getName());
                groupVo.setType(GroupTypeEnums.SUB_VIEW.getCode());
                groupVo.setTypeName(GroupTypeEnums.SUB_VIEW.getValue());
                groupVo.setParentId(view.getId());
                groupVo.setView(child);
                groupVos.add(groupVo);
            }
        }
        return groupVos;
    }
}